package org.luxor.cloud.gateway.component.security;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;

/**
 * 当前登陆用户
 *
 * @author Mr.Yan  @date 2020/3/31
 */
public class UserSubject implements UserDetails {
    /**
     * 机构编号
     */
    @JSONField(serializeUsing = ToStringSerializer.class)
    private Long tenantId;

    /**
     * 部门编号
     */
    @JSONField(serializeUsing = ToStringSerializer.class)
    private Long deptId;

    /**
     * 用户编号
     */
    @JSONField(serializeUsing = ToStringSerializer.class)
    private Long userId;

    /**
     * 用户姓名
     */
    private String realName;

    /**
     * 用户编码(唯一登陆账号)
     */
    private String username;

    /**
     * 用户密码
     */
    private String password;

    /**
     * 所拥有的权限集
     */
    private Collection<GrantedAuthority> authorities;

    private boolean enabled;
    private boolean accountNonLocked;
    private boolean accountNonExpired;
    private boolean credentialsNonExpired;

    public UserSubject() {
        this.tenantId = 0L;
        this.deptId = 0L;
        this.userId = 0L;
        this.username = StringUtils.EMPTY;
        this.realName = StringUtils.EMPTY;
        this.password = StringUtils.EMPTY;
        this.enabled = true;
        this.accountNonLocked = true;
        this.accountNonExpired = true;
        this.credentialsNonExpired = true;
        this.authorities = new HashSet<>(5);
    }

    public UserSubject(Long tenantId, Long deptId, Long userId, String username, String realName) {
        this.tenantId = tenantId;
        this.deptId = deptId;
        this.userId = userId;
        this.username = username;
        this.realName = realName;
        this.password = StringUtils.EMPTY;
        this.enabled = true;
        this.accountNonLocked = true;
        this.accountNonExpired = true;
        this.credentialsNonExpired = true;
        this.authorities = new HashSet<>(5);
    }

    public String toJson() {
        return "{\"tenantId\":\"" + this.tenantId + "\","
                + "\"deptId\":\"" + this.deptId + "\","
                + "\"userId\":\"" + this.userId + "\","
                + "\"username\":\"" + this.username + "\","
                + "\"realName\":\"" + this.realName + "\"}";
    }

    @Override
    public String toString() {
        return "UserSubject{" +
                "tenantId='" + tenantId + '\'' +
                ", deptId='" + deptId + '\'' +
                ", userId='" + userId + '\'' +
                ", username='" + username + '\'' +
                ", realName='" + realName + '\'' +
                '}';
    }

    public Long getTenantId() {
        return tenantId;
    }

    public void setTenantId(Long tenantId) {
        this.tenantId = tenantId;
    }

    public Long getDeptId() {
        return deptId;
    }

    public void setDeptId(Long deptId) {
        this.deptId = deptId;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    public void setAuthorities(List<GrantedAuthority> authorities) {
        this.authorities = authorities;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.accountNonLocked;
    }

    public void setAccountNonLocked(boolean accountNonLocked) {
        this.accountNonLocked = accountNonLocked;
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.accountNonExpired;
    }

    public void setAccountNonExpired(boolean accountNonExpired) {
        this.accountNonExpired = accountNonExpired;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.credentialsNonExpired;
    }

    public void setCredentialsNonExpired(boolean credentialsNonExpired) {
        this.credentialsNonExpired = credentialsNonExpired;
    }


}
